package org.leetcode.easy.leetcode997;

import java.util.Arrays;

public class Solution
{
    public static int[] sortedSquares(int[] nums) {

        int []result = new int[nums.length];
        int left = 0;
        int right = nums.length-1;
        int resultIndex = nums.length-1;

        while (resultIndex>=0)
        {
            if (nums[left]*nums[left]<=nums[right]*nums[right])
            {
                result[resultIndex]=nums[right]*nums[right];
                right--;
            }else {
                result[resultIndex]=nums[left]*nums[left];
                left++;
            }

            resultIndex--;
        }


        return result;
    }


    public static void main(String[] args) {
        int [] nums ={-4,-1,0,3,10};

        int[] squares = sortedSquares(nums);
        System.out.println(Arrays.toString(squares));
    }
    }
